/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <aygshell.h>

#include "Dialog.h"
#include "Resources.h"

Dialog::Dialog(HINSTANCE hInstance, const TCHAR *caption, CommandSet cs) throw(): m_hInstance(hInstance), m_hWnd(0), m_hWndMb(0), m_hMenu(0), m_cs(cs), m_close(false), m_cc(CC_OK) {
    WNDCLASS wc;
    if(!GetClassInfo(m_hInstance, CLASSNAME, &wc)) {
        ZeroMemory(&wc, sizeof(WNDCLASS));
        wc.lpfnWndProc = wndProc;
        wc.hInstance = m_hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
        wc.lpszClassName = CLASSNAME;
        RegisterClass(&wc);
    }
    
    m_hWnd = CreateWindow(CLASSNAME, caption, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, m_hInstance, this);

    m_hMenu = HMENU(SendMessage(m_hWndMb, SHCMBM_GETSUBMENU, 0, 0));
    RemoveMenu(m_hMenu, 0, MF_BYPOSITION);
}

Dialog::CloseCommand Dialog::open() throw() {
    ShowWindow(m_hWnd, SW_SHOW);
    ShowWindow(m_hWndMb, SW_SHOW);
    
    MSG msg;
    while(!m_close && GetMessage(&msg, 0, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    ShowWindow(m_hWndMb, SW_HIDE);
    ShowWindow(m_hWnd, SW_HIDE);

    return(m_cc);
}

unsigned int Dialog::width() const throw() {
    RECT rect;
    GetClientRect(m_hWnd, &rect);
    LONG dlgBase = LOWORD(GetDialogBaseUnits());
    return(MulDiv(rect.right - rect.left, 4, dlgBase));
}

unsigned int Dialog::height() const throw() {
    RECT rect;
    GetClientRect(m_hWnd, &rect);
    LONG dlgBase = HIWORD(GetDialogBaseUnits());
    return(MulDiv(rect.bottom - rect.top, 8, dlgBase));
}

HMENU Dialog::enableMenu(const TCHAR *title) throw() {
    TBBUTTONINFO bi;
    ZeroMemory(&bi, sizeof(TBBUTTONINFO));
    bi.cbSize = sizeof(TBBUTTONINFO);
    bi.dwMask = TBIF_TEXT | TBIF_STATE;
    bi.pszText = LPWSTR(title);
    bi.fsState = TBSTATE_ENABLED;
    SendMessage(m_hWndMb, TB_SETBUTTONINFO, 0, LPARAM(&bi));

    return(m_hMenu);
}

HINSTANCE Dialog::hInstance() throw() {
    return(m_hInstance);
}

HWND Dialog::hWnd() throw() {
    return(m_hWnd);
}

unsigned int Dialog::widthToPixels(unsigned int width) throw() {
    LONG dlgBase = LOWORD(GetDialogBaseUnits());
    return(MulDiv(width, dlgBase, 4));
}

unsigned int Dialog::heightToPixels(unsigned int height) throw() {
    LONG dlgBase = HIWORD(GetDialogBaseUnits());
    return(MulDiv(height, dlgBase, 8));
}

unsigned int Dialog::widthToDialogUnits(unsigned int width) throw() {
    LONG dlgBase = LOWORD(GetDialogBaseUnits());
    return(MulDiv(width, 4, dlgBase));
}

unsigned int Dialog::heightToDialogUnits(unsigned int height) throw() {
    LONG dlgBase = HIWORD(GetDialogBaseUnits());
    return(MulDiv(height, 8, dlgBase));
}

LRESULT CALLBACK Dialog::wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    if(uMsg == WM_CREATE) {
        CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lParam);
        Dialog *dlg = reinterpret_cast<Dialog *>(cs->lpCreateParams);
        SetWindowLong(hWnd, GWL_USERDATA, LONG(dlg));
        
        SHINITDLGINFO idi;
        idi.dwMask = SHIDIM_FLAGS;
        idi.hDlg = hWnd;
        switch(dlg->m_cs) {
            case CS_CLOSE:
                idi.dwFlags = SHIDIF_CANCELBUTTON;
                break;
            case CS_OKCANCEL:
                idi.dwFlags = SHIDIF_DONEBUTTON;
                break;
        }
        idi.dwFlags |= SHIDIF_SIZEDLGFULLSCREEN;
        SHInitDialog(&idi);

        SHMENUBARINFO mbi;
        ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
        mbi.cbSize = sizeof(SHMENUBARINFO);
        mbi.hwndParent = hWnd;
        mbi.dwFlags = SHCMBF_HMENU;
        mbi.nToolBarId = ID_TOOLBAR;
        mbi.hInstRes = GetModuleHandle(0);
        SHCreateMenuBar(&mbi);
        dlg->m_hWndMb = mbi.hwndMB;
        ShowWindow(mbi.hwndMB, SW_HIDE);

        switch(dlg->m_cs) {
            case CS_OKCANCEL:{
                TBBUTTONINFO bi;
                ZeroMemory(&bi, sizeof(TBBUTTONINFO));
                bi.cbSize = sizeof(TBBUTTONINFO);
                bi.dwMask = TBIF_TEXT | TBIF_STATE;
                bi.pszText = TEXT("Cancel");
                bi.fsState = TBSTATE_ENABLED;
                SendMessage(mbi.hwndMB, TB_SETBUTTONINFO, ID_BUTTON, LPARAM(&bi));
            }
        }

        return(0);
    }
    else {
        Dialog *dlg = reinterpret_cast<Dialog *>(GetWindowLong(hWnd, GWL_USERDATA));
        if(dlg != 0) {
            HWND hMB = SHFindMenuBar(dlg->m_hWnd);
            switch(uMsg) {
                case WM_COMMAND:
                    if(HIWORD(wParam) == 0) {
                        if(lParam != 0 && LOWORD(wParam) == ID_BUTTON && dlg->m_cs == CS_OKCANCEL && dlg->close(CC_CANCEL)) {
                            // From the menubar cancel button.
                            dlg->m_close = true;
                            dlg->m_cc = CC_CANCEL;
                        }
                        else {
                            // From a menu.
                            dlg->menuNotification(LOWORD(wParam));
                        }
                    }
                    else {
                        if(lParam == 0) {
                            if(LOWORD(wParam) == IDOK && dlg->close(CC_OK)) {
                                dlg->m_close = true;
                                dlg->m_cc = CC_OK;
                            }
                            else if(LOWORD(wParam) == IDCANCEL && dlg->close(CC_CANCEL)) {
                                dlg->m_close = true;
                                dlg->m_cc = CC_CANCEL;
                            }
                        }
                        else {
                            dlg->controlNotification(HWND(lParam), HIWORD(wParam));
                        }
                    }
                    return(0);
                default:
                    return(DefWindowProc(hWnd, uMsg, wParam, lParam));
            }
        }
        else {
            return(DefWindowProc(hWnd, uMsg, wParam, lParam));
        }
    }
}

const TCHAR * const Dialog::CLASSNAME = TEXT("DamDialogClass");
